<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList" :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              max-height="500px"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
            >
            <el-table-column
                label="姓名"
                align="center"
                prop="nickname"
                width="180px"
                :show-overflow-tooltip="true"
            />
            <el-table-column label="手机号码" align="center" prop="phone" width="120px"/>
<!--            邀请码-->
            <el-table-column label="邀请二维码" align="center" prop="invite_qr_code" width="120px">
              <template #default="scope">
                <el-image
                  style="width: 80px; height: 80px"
                  :src="scope.row.invite_qr_code"
                  :preview-src-list="[scope.row.invite_qr_code]"
                ></el-image>
              </template>
            </el-table-column>
            <el-table-column label="所属上级" align="center" width="180px">
              <template #default="scope">
                <template v-if="scope.row.parent">
                  <div>{{ scope.row.parent.nickname }}</div>
                  <el-tag
                    size="mini"
                    :type="getParentTypeTag(scope.row.parent.user_type)"
                  >
                    {{ getParentTypeLabel(scope.row.parent.user_type) }}
                  </el-tag>
                </template>
                <span v-else>-</span>
              </template>
            </el-table-column>
            <el-table-column label="用户类型" align="center" prop="user_type" width="120px">
              <template #default="scope">
                <el-select
                  :value="scope.row.user_type"
                  size="mini"
                  @change="(value) => handleUserTypeChange(scope.row, value)"
                  placeholder="请选择用户类型">
                  <el-option
                    v-for="item in userTypeOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="状态" key="status" width="100px">
              <template #default="scope">
                <el-tag v-if="scope.row.status === CommonStatusEnum.ENABLE" type="success">启用</el-tag>
                <el-tag v-else type="danger">禁用</el-tag>
              </template>
            </el-table-column>
            <el-table-column
                label="创建时间"
                align="center"
                prop="create_time"
                width="180"
            />
            <el-table-column label="操作" fixed="right" width="200px">
              <template slot-scope="scope">
                <el-button type="text" @click="onEdit2(scope.row)">生成邀请码</el-button>
                <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
      <div class="page-box">
        <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
        />
      </div>
    </div>

    <Add ref="addFormRef" @get-page="initData" />
  </div>
</template>
<script>

import crud from "@/mixins/crud";
import { http } from "@/api";
import Add from "./add.vue";
import {CommonStatusEnum, CommonUserTypeEnum} from "@/utils/enum";
export default {
  name: "index",
  computed: {
    CommonStatusEnum() {
      return CommonStatusEnum
    },
    CommonUserTypeEnum() {
      return CommonUserTypeEnum
    }
  },
  components: {
    Add,
  },
  mixins: [crud],
  data() {
    return {
      request:http.user,
      userTypeOptions: [
        { label: '普通用户', value: CommonUserTypeEnum.NORMAL_USER },
        { label: '商家', value: CommonUserTypeEnum.BUSINESS },
        { label: '一级分销商', value: CommonUserTypeEnum.DISTRIBUTOR_LEVEL1 },
        { label: '二级分销商', value: CommonUserTypeEnum.DISTRIBUTOR_LEVEL2 }
      ],
      searchList: [
        {
          label: '姓名',
          prop: 'nickname',
          name: 'input',
          clearable: true
        },
        {
          label: '手机号',
          prop: 'phone',
          name: 'input',
          clearable: true
        },
        {
          label: '用户类型',
          prop: 'user_type',
          name: 'select',
          clearable: true,
          options: [
            { label: '普通用户', value: CommonUserTypeEnum.NORMAL_USER },
            { label: '商家', value: CommonUserTypeEnum.BUSINESS },
            { label: '一级分销商', value: CommonUserTypeEnum.DISTRIBUTOR_LEVEL1 },
            { label: '二级分销商', value: CommonUserTypeEnum.DISTRIBUTOR_LEVEL2 }
          ]
        },
        {
          label: '状态',
          prop: 'status',
          name: 'select',
          clearable: true,
          options: [
            { label: '启用', value: CommonStatusEnum.ENABLE },
            { label: '禁用', value: CommonStatusEnum.DISABLE }
          ]
        },
      ],
      searchParams: {
        nickname: '',
      },
      tableData: [

      ]
    }
  },
  methods: {
    getParentTypeTag(type) {
      switch (type) {
        case CommonUserTypeEnum.NORMAL_USER:
          return 'info';
        case CommonUserTypeEnum.BUSINESS:
          return 'warning';
        case CommonUserTypeEnum.DISTRIBUTOR_LEVEL1:
          return 'success';
        case CommonUserTypeEnum.DISTRIBUTOR_LEVEL2:
          return 'primary';
        default:
          return 'info';
      }
    },
    getParentTypeLabel(type) {
      switch (type) {
        case CommonUserTypeEnum.NORMAL_USER:
          return '普通用户';
        case CommonUserTypeEnum.BUSINESS:
          return '商家';
        case CommonUserTypeEnum.DISTRIBUTOR_LEVEL1:
          return '一级分销商';
        case CommonUserTypeEnum.DISTRIBUTOR_LEVEL2:
          return '二级分销商';
        default:
          return '未知';
      }
    },
    handleUserTypeChange(row, value) {
      // 这里可以调用API更新用户类型
      this.$confirm('确认修改该用户的类型吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        http.user.update(row.id, { user_type: value }).then(() => {
          this.$message.success('修改成功');
          row.user_type = value;
        })
      })
    },
    onEdit2(row) {
      this.loading = true
      http.user.getUserQrCode({
        user_id: row.id
      }).then(res => {
        this.$message.success('获取二维码成功')
        this.onSearch()
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>

<style scoped lang="scss">

</style>
